--[[
从报告通知队列KEYS[1]中读取[序列:号码],并读取其关联[KEYS[2]:序号:号码]HASH中的内容,返回指定长度的List

KEYS[1] : 通知队列
KEYS[2] : hash key前缀, 和KEYS[1]读取的字符串组成完整的hash key
ARGV[1] : 返回的最大list长度

--]]

function string.split(str, delimiter)
        if str==nil or str=='' or delimiter==nil then
                return nil
        end

    local result = {}
    for match in (str..delimiter):gmatch("(.-)"..delimiter) do
        table.insert(result, match)
    end
    return result
end

local notifylist = KEYS[1]
local hashprefix = KEYS[2]
local count = tonumber(ARGV[1])
local array = {}
local index = 0

for i=1, count, 1 do
    local seqnumtype = redis.call("rpop", notifylist)
    if (seqnumtype ~= false and seqnumtype ~= "") then
        local seqnum = string.split(seqnumtype, ":")
        local sminfo = redis.call("hmget", hashprefix..":"..seqnum[1]..":"..seqnum[2], "Srv_node", "ServiceCode", "User", "GateLinkId", "SendResult", "RecvErrorCode", "DealResult", "Reportflag", "SerialSegment", "SendPort", "OrgSrvNode", "OrgSerialNo")
        index = index + 1
        table.insert(sminfo, seqnum[3])
        table.insert(sminfo, seqnumtype)
        array[index] = sminfo
    else
        break
    end

    if (index >= count) then
        break
    end
end

return array